﻿using Corina.Commands;
using Corina.Common;
using Corina.Enumerations;
using Corina.Infrastructure;
using Corina.Win.Helpers;
using Corina.Win.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Corina.Win.ServerHub.Viewmodels
{
    public class PageViewmodelBase<T> : ViewmodelBase, IView where T : class
    {
        public PageViewmodelBase()
        {
            
        }

        #region Fields
        private int _pageIndex = 0;
        private string _title;
        private string _uriString;
        private UriType _uriType = UriType.OutApp;
        private string _icon;
        #endregion

        #region Properties
        public virtual int PageIndex
        {
            get { return _pageIndex; }
            set {
                _pageIndex = value;
                RaisePropertyChanged(() => PageIndex);
            }
        }

        public virtual string Icon
        {
            get { return _icon; }
            set
            {
                _icon = value;
                RaisePropertyChanged(() => Icon);
            }
        }

        public virtual string Title {
            get { return _title; }
            set {
                _title = value;
                RaisePropertyChanged(() => Title);
            }
        }

        public virtual string UriString
        {
            get { return _uriString; }
            set
            {
                _uriString = value;
                RaisePropertyChanged(() => UriString);
                RaisePropertyChanged(() => FullUrl);
            }
        }

        public virtual UriType UriType
        {
            get { return _uriType; }
            set {
                _uriType = value;
                RaisePropertyChanged(() => UriType);
                RaisePropertyChanged(() => FullUrl);
            }
        }

        public virtual string FullUrl
        {
            get
            {
                return UrlPrefix<T>.Prefixs.FirstOrDefault(k => k.Key == UriType).Value + "Views/" + UriString + ".xaml";
            }
        }
        #endregion

        protected virtual void LoadData(params object[] paras)
        { 

        }

        #region Pagination
        protected virtual void CurrentIndexChanged()
        {
            LoadData();
        }

        private int _index = 1;
        public int Index
        {
            get { return _index; }
            set
            {
                _index = value;
                RaisePropertyChanged(() => Index);
                CurrentIndexChanged();
                RaisePropertyChanged(() => FirstPageCommand);
                RaisePropertyChanged(() => PreviousPageCommand);
                RaisePropertyChanged(() => NextPageCommand);
                RaisePropertyChanged(() => LastPageCommand);
            }
        }

        private int _itemsCount = 0;
        public int ItemsCount
        {
            get { return _itemsCount; }
            set
            {
                _itemsCount = value;
                RaisePropertyChanged(() => ItemsCount);
                RaisePropertyChanged(() => PagesCount);
                RaisePropertyChanged(() => FirstPageCommand);
                RaisePropertyChanged(() => PreviousPageCommand);
                RaisePropertyChanged(() => NextPageCommand);
                RaisePropertyChanged(() => LastPageCommand);
            }
        }

        public int PagesCount
        {
            get
            {
                return ItemsCount > 0 ? ((ItemsCount + 19) / 20) : 1;
            }
        }

        public ICommand FirstPageCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Index > 1; },
                    ExecuteCallback = delegate
                    {
                        Index = 1;
                    }
                };
            }
        }

        public ICommand PreviousPageCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Index > 1; },
                    ExecuteCallback = delegate
                    {
                        Index--;
                    }
                };
            }
        }

        public ICommand NextPageCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Index < PagesCount; },
                    ExecuteCallback = delegate
                    {
                        Index++;
                    }
                };
            }
        }

        public ICommand LastPageCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return Index < PagesCount; },
                    ExecuteCallback = delegate
                    {
                        Index = PagesCount;
                    }
                };
            }
        }
        #endregion
    }
}
